<?php

namespace App\Http\Controllers\Merchant;

use App\Exports\UserAnalysisExport;
use App\Http\Response;
use App\UserAcm;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class UserAcmController extends Controller
{
    use Response;
    public function index(Request $request)
    {
       $start_time=$request->get('start_time');
       $end_time=$request->get('end_time',Carbon::now());
       if ($start_time||$end_time){
           $data=UserAcm::whereBetween('date',[$start_time,$end_time])->where('mch_id',auth('merchant')->id())->paginate($request->get('pageSize'),'*','pageNo');
           return $this->success(200,$data);

       }else{
           return $this->error(505,trans('topic.param_error'));
       }
    }
    public function analysis()
    {
        $now=Carbon::now()->format('Y-m-d');
        $data=UserAcm::where('date',$now)->first();
        $result=[
            'new'=>$data?$data['register_number']:0,
            'daily_pv'=>$data?$data['pv']:0,
            'accumulate_register'=>$data?$data['accumulate_register']:0
        ];
        return $this->success(200,$result);
    }
    public function export(Request $request)
    {
        $start_time=$request->get('start_time');
        $end_time=$request->get('end_time',Carbon::now());

        return Excel::download(new UserAnalysisExport($start_time,$end_time),time()."userAnalysis.xls");
    }
}
